<?php

namespace app\modules\v1\controllers;

use Codeception\Module\Yii1;
use yii\web\Controller;
use Yii;
use yii\web\Request;

/**
 * 订单接口
 * Default controller for the `v1` module
 */
class OrderController extends Controller
{

    /**
     * 获取地址
     * @return false|string
     * @throws \yii\db\Exception
     */
    public function actionGetconsignee()
    {
        $request = Yii::$app->request;
        if (!$request->isGet) {
            return json_encode([
                'error_code' => 1003,
                'msg' => '请求方法为GET',
            ]);
        }
        $id = $request->get('u_id', '');
        if ($id == '') {
            return json_encode([
                'error_code' => 1002,
                'msg' => '请求参数错误',
            ]);
        }

        $data = Yii::$app->db->
        createCommand("select consignee_name,consignee_phone,consignee_address from consignee where user_id=:id")
            ->bindValue(':id', $id)
            ->queryAll();
        return json_encode([
            'error_code' => 1000,
            'data' => $data,
        ]);
    }

    /**
     * 添加地址
     * @return false|string
     */
    public function actionAddconsignee()
    {
        $request = Yii::$app->request;
        if (!$request->isPost) {
            return json_encode([
                'error_code' => 1003,
                'msg' => '请求方法为POST',
            ]);
        }
        $consignee_name = $request->post('consignee_name', '');
        $consignee_phone = $request->post('consignee_phone', '');
        $consignee_address = $request->post('consignee_address', '');
        if ($consignee_address == '' || $consignee_name == '' || $consignee_phone == '') {
            return json_encode([
                'error_code' => 1002,
                'msg' => '请求参数错误',
            ]);
        }
        $res = Yii::$app->db->createCommand()
            ->insert('consignee', [
                'consignee_name' => $consignee_name,
                'consignee_phone' => $consignee_phone,
                'consignee_address' => $consignee_address,
            ])->execute();
        if ($res) {
            return json_encode([
                'error_code' => 1000,
                'msg' => '成功',
            ]);
        } else {
            return json_encode([
                'error_code' => 1001,
                'msg' => '失败',
            ]);
        }
    }

    /**
     * 添加订单
     * @return false|string
     * @throws \yii\db\Exception
     */
    public function actionAddorder()
    {
        $request = Yii::$app->request;
        if (!$request->isPost) {
            return json_encode([
                'error_code' => 1003,
                'msg' => '请求方法为POST',
            ]);
        }
        $number = date('Ymd', time()) . time();
        $shop = $request->post('shop', '');
        $shopNum = $request->post('shopNum', '');
        $status = $request->post('status', 2);
        $amount = $request->post('amount', '');
        $userid = $request->post('userid', '');
        $consigneeid = $request->post('cid', '');
        $time = date('Y-m-d h:i:s', time());
        if ($shop == '' || $amount == '' || $userid == '' || $shopNum == '' || $consigneeid == '') {
            return json_encode([
                'error_code' => 1002,
                'msg' => '请求参数错误',
            ]);
        }

        $ashop = explode(',', $shop);
        $ashopNum = explode(',', $shopNum);
        $count = count($ashop);
        $shop_name = '';
//        print_r($ashop);die;
        for ($i = 0; $i <= $count - 1; $i++) {
            $str = $ashop[$i];
            $num = $ashopNum[$i];
            $shop_name .= $str . "-" . $num . ",";
        }
        $str = rtrim($shop_name, ',');
        $res = Yii::$app->db->createCommand()
            ->insert('shop_order', [
                'shop_name' => $str,
                'order_number' => $number,
                'order_time' => $time,
                'submitter' => $userid,
                'order_status' => $status,
                'order_amount' => $amount,
                'order_type' => '普通',
            ])->execute();
        if ($res) {
            return json_encode([
                'error_code' => 1000,
                'msg' => '成功',
            ]);
        } else {
            return json_encode([
                'error_code' => 1001,
                'msg' => '失败',
            ]);
        }
    }
    /**
     *
     */
    public function actionGetone()
    {
        $request = Yii::$app->request;
      $zid = $request->get('id');
        if (!$request->isGet) {
            return json_encode([
                'error_code' => 1003,
                'msg' => '请求方法为GET',
            ]);
        }
        if (!$zid) {
            return json_encode([
                'error_code' => 1002,
                'msg' => '参数错误',
            ]);
        }
        $data = Yii::$app->db->createCommand("select * from shop_order as s left join consignee as c on s.consignee_id=c.consignee_id LEFT join express as e on s.express_id=e.express_id where s.order_id=:id")
            ->bindValue(':id', $zid)
            ->queryOne();
        if ($data){
            $uid = $data['submitter'];
            $user = Yii::$app->db->createCommand("select * from users where id=:id")
                ->bindValue(':id', $uid)
                ->queryOne();
            $data['submitter'] = $user;
            $arr = explode(',',$data['shop_name']);
            foreach ($arr as $value) {
                $idALL[] = explode('-', $value);
            }
            foreach ($idALL as $shop) {
                $good = Yii::$app->db->createCommand("select  shop_id,shop_name,shop_img from shop where shop_id=:id")
                    ->bindValue(':id', $shop[0])
                    ->queryOne();
                $good['num'] = $shop[1];
                $goods[] = $good;
            }
        }else{
            return json_encode([
                'error_code' => 1000,
                'msg' => '没有此数据',
            ]);
        }

        $data['shop_name'] = $goods;
        return json_encode([
            'error_code' => 1000,
            'data' => $data,
        ]);
    }

    /**
     * 订单获取
     * @return false|string
     * @throws \yii\db\Exception
     */
    public function actionGetorder()
    {
        $zid = Yii::$app->request->get('id');
        $request = Yii::$app->request;
        if (!$request->isGet) {
            return json_encode([
                'error_code' => 1003,
                'msg' => '请求方法为GET',
            ]);
        }
        if (!$zid) {
            return json_encode([
                'error_code' => 1002,
                'msg' => '参数错误',
            ]);
        }
        $db = Yii::$app->db;
        $data = $db->
        createCommand("select * from shop_order where submitter=:id")
            ->bindValue(':id', $zid)
            ->queryAll();
        if ($data == []) {
            return json_encode([
                'error_code' => 1000,
                'data' => $data,
            ]);
            die;
        }
        foreach ($data as $k => $v) {
            $id = $v['submitter'];
            $consignee_id = $v['consignee_id'];
            $name = Yii::$app->db->createCommand("select * from users where id=:id")
                ->bindValue(':id', $id)
                ->queryOne();
            $data[$k]['submitter'] = $name['u_name'];
            $consignee = Yii::$app->db->createCommand("select * from consignee where consignee_id=:id")
                ->bindValue(':id', $consignee_id)
                ->queryOne();
            $data[$k]['consignee_id'] = $consignee;
            $arr = explode(',', $v['shop_name']);
            foreach ($arr as $value) {
                $idALL[] = explode('-', $value);
            }
            foreach ($idALL as $shop) {
                $good = Yii::$app->db->createCommand("select  shop_id,shop_name,shop_img from shop where shop_id=:id")
                    ->bindValue(':id', $shop[0])
                    ->queryOne();
                $good['num'] = $shop[1];
                $goods[] = $good;
            }
            $data[$k]['shop_name'] = $goods;
            $idALL = [];
            $goods = [];
        }
        return json_encode([
            'error_code' => 1000,
            'data' => $data,
        ]);
    }

    /**
     * 地址删除
     * @return false|string
     */
    public function actionDelconsignee()
    {
        $request = Yii::$app->request;
        if (!$request->isGet) {
            return json_encode([
                'error_code' => 1003,
                'msg' => '请求方法为POST',
            ]);
        }
        $id = $request->get('id', '');
        if ($id=='') {
            return json_encode([
                'error_code' => 1002,
                'msg' => '请求参数为空',
            ]);
        }

        $res = Yii::$app->db->createCommand()
            ->delete('consignee', 'consignee_id = '.$id)->execute();
        if ($res) {
            return json_encode([
                'error_code' => 1000,
                'msg' => '成功',
            ]);
        } else {
            return json_encode([
                'error_code' => 1001,
                'msg' => '失败',
            ]);
        }
    }

    /**
     * 删除订单
     * @return false|string
     * @throws \yii\db\Exception
     */
    public function actionDelorder()
    {
        $request = Yii::$app->request;
        if (!$request->isGet) {
            return json_encode([
                'error_code' => 1003,
                'msg' => '请求方法为POST',
            ]);
        }
        $id = $request->get('id', '');
        if ($id=='') {
            return json_encode([
                'error_code' => 1002,
                'msg' => '请求参数为空',
            ]);
        }

        $res = Yii::$app->db->createCommand()
            ->delete('shop_order', 'order_id = '.$id)->execute();
        if ($res) {
            return json_encode([
                'error_code' => 1000,
                'msg' => '成功',
            ]);
        } else {
            return json_encode([
                'error_code' => 1001,
                'msg' => '失败',
            ]);
        }
    }

}
